Thread: Memory for Char*[x] - How to get the proper amount?

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    30

    Memory for Char*[x] - How to get the proper amount?

    Hi,

    I've got an array like this:

    tmparr char*[some number]

    Since number must be constant, I thought I just declare the array somehow and I reallocate the necessary space for it later using malloc, or calloc.

    However I don't know how to do this, because void* cannot be converted to char*[]! Calloc would be cool, as it allocates space for an array of x elements, so it's just what I need (I guess -.

    Or is there any other (better) method?

    Also please somebody tell me what is difference between char** and char[]*! The latter is an array of pointers to strings, right? Then the first one would be the array of strings? Probably it's a stupid question, but I'm still very new to this C stuff.


    Thanks for any help!
    Last edited by Bill 101; 11-11-2002 at 12:29 PM.
    Best Regards,

    Bill

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You have to cast it:
    Code:
    int Size = 12345;
    char* Array;
    
    Array = (char*)malloc(Size * sizeof(char));
    
    if(Array != NULL)
    {
    
       ...
    
       free(Array);
    }
    else
    {
       printf("Not enough memory!");
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    There is no need for casting. A void-pointer can be assigned to any other kind of pointer, it is a general pointer.

    You are correct about char** and char *[].

    char **p;

    Here p is a pointer to pointer to char.

    char *p[n];

    Here p is an array of n pointers to char.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    30
    Hi,

    Thanks for the answer.

    Yes, I knew that it is possbile to cast char* to void* and vice-versa, however my array is a char*[x]!

    If I type in:

    char *tmparr[] = (char*[]) malloc(some size + 1);

    or

    char *tmparr[] = (char*[]) calloc(count, count * somesize);

    then the compiler (Borland C++ 6) will scream! -

    I also tried to use char** instead of char*[num]. That is better, because I was able to cast it to void*, but in this case the size of the array will be always 4 (at least that's what sizeof says...)

    What am I doing wrong?
    Best Regards,

    Bill

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    This

    >char *tmparr[] = (char*[]) malloc(some size + 1);

    is not correct.

    Look at the example of Magos. Magos allocated memory for an array. You have an array of pointers to char, which can be used for an array of strings. Each element of the array is a string.

    To allocate memory for one string, you can use the method of Magos. For an array of strings, you need to allocate memory for each string in the array. It would look like this:

    Code:
    char *string_array [NR_OF_STRINGS];
    
    for (i = 0; i < NR_OF_STRINGS; i++)
    {
        /* Allocate memory for the i-th string. */
        string_array [i] = malloc (STRING_LENGTH);
    }
    And don't forget to release the memory when you don't need it anymore, you can use the function free() for this.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >char *string_array [NR_OF_STRINGS];
    >string_array [i] = malloc (STRING_LENGTH);
    Two things:
    - string_array is a reserved name, so don't use it in your real code.
    - STRING_LENGTH would imply strlen(myvar) would do the job. But it wouldn't, you must remember to include +1 for the null terminator.

    Here's a working example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define COUNT 5
    
    int main(void)
    {
        char *my_ary[COUNT];
        char buf[BUFSIZ];
        int i;
        
        for (i = 0; i < COUNT; i++)
        {
            /* You'd normally do error checking with this lot */
            fgets(buf, sizeof buf, stdin);
            my_ary[i] = malloc(strlen(buf)+1);
            strcpy(my_ary[i], buf);
        }
    	
        for (i = 0; i < COUNT; i++)
        {
            printf("%s", my_ary[i]);
            free(my_ary[i]);
        }
    	
        return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    According to my copy of the C standard, there is no such as string_array. Where did you get this from?

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Shiro
    According to my copy of the C standard, there is no such as string_array. Where did you get this from?
    It's in the "Future library directions" section (7.26 in c99). More explicitly, sections 7.26.10 and 7.26.11. It isn't mentioned by name, only that anything starting str followed by a lowercase letter is reserved.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    whaaa? So it isn't "right" to name a variable strike, strong_man, strength, stripes and such?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It's been discussed a few times before.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    30
    Hi!

    Ok I see, now it's clear. Thank you very much for your help!
    Best Regards,

    Bill

  12. #12
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >It's in the "Future library directions" section (7.26 in c99). More
    >explicitly, sections 7.26.10 and 7.26.11. It isn't mentioned by
    >name, only that anything starting str followed by a lowercase
    >letter is reserved.

    OK, I see, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Having a problem!
    By Zildjian in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2004, 09:40 AM
  3. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM